﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using ArkEC.SEMs.DataAccess.MLM;
using ArkEC.SEMs.DataAccess;
using EssHelper.Helper;
using System.IO;
using ArkEC.CoreCom.CacheStore;

namespace ArkEC.SEMs.Business.MLM
{
    /// <summary>
    /// 多语言业务类
    /// </summary>
    public class LanTypeService
    {
        /// <summary>
        /// 导入语言种类
        /// </summary>
        /// <remarks>
        /// 1. 删除所有的语言种类；
        /// 2. 检测数据合法性（语言代码和语言名称不能为空，并且不能重复）；
        /// 3. 将excel中的所有语言种类新增到数据库中；
        /// 4. 将非法数据记录到临时文件中（数据列表中的增加一列，记录异常提示）；
        /// 5. 返回临时文件的相对路径；
        /// </remarks>
        /// <param name="excelPath">导入文件的临时存放路径</param>
        /// <returns></returns>
        public static string Import(string excelPath)
        {
            string errorPath = AppDomain.CurrentDomain.BaseDirectory;

            return errorPath;
        }

        /// <summary>
        /// 导出所有语言种类
        /// </summary>
        /// <returns></returns>
        public static string Export()
        {
            string templatePath = AppDomain.CurrentDomain.BaseDirectory + @"Database\File\ExcelTemplate\MultiLanguage.xls";

            string guid = Guid.NewGuid().ToString();
            //用以Web下载的相对路径
            string relativePath = @"/Database/File/Temp/MultiLanguage(" + guid + ").xls";

            //复制模板到临时目录中
            string excelPath = AppDomain.CurrentDomain.BaseDirectory + @"Database\File\Temp\MultiLanguage(" + guid + ").xls"; ;
            File.Copy(templatePath, excelPath, true);

            DataTable dt = null;
            using (DbConnection conn = ConnFactory.CreateConn())
            {
                dt = MLMDAFactory.CreateLanTypeDA().GetLanTypes(conn, null);
            }

            //设置多语言列头
            foreach (DataColumn column in dt.Columns)
            {
                column.ColumnName = LanResCache.GetLanValue(column.ColumnName);
            }

            ExcelManager.WriteToExcelFile(excelPath, dt.DataSet);

            return relativePath;
        }

        /// <summary>
        /// 获取所有语言种类
        /// </summary>
        /// <returns></returns>
        public static DataTable GetAllLanTypes()
        {
            DataTable dt = WebCacheHelper.GetItem(LanTypeCache.Instance, "Default") as DataTable;
            return dt;
        }
    }
}
